package com.capgemini.remind.service;

import com.capgemini.commons.IdUtils;
import com.capgemini.core.PageList;
import com.capgemini.remind.mapper.RemindSmsMapper;
import com.capgemini.remind.pojo.Announcement;
import com.capgemini.remind.pojo.RemindSms;
import com.capgemini.remind.pojo.SmsQuota;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author
 * @date
 */
@Service
@Transactional
public class RemindSmsService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RemindSmsMapper remindSmsMapper;

	public void queryPage(final PageList<RemindSms> pageList) {
		pageList.setItems(remindSmsMapper.queryPage(pageList));
	}

	public Long querySmsQuantityByDealerNum(String dealerNum){
		return remindSmsMapper.querySmsQuantityByDealerNum(dealerNum);
	}


	public void subtractSmsQuotaByDealerNum(Integer num, String dealerNum) {
		SmsQuota smsQuota = remindSmsMapper.getSmsQuotaByDealerNumForUpdate(dealerNum);

		Long tmp = (smsQuota.getDefaultQuantity() - num);
		smsQuota.setDefaultQuantity(tmp);
		if (tmp < 0) {
			smsQuota.setDefaultQuantity(0L);
			smsQuota.setPurchaseQuantity(smsQuota.getPurchaseQuantity() - Math.abs(tmp));
		}

		remindSmsMapper.updateSmsQuotaByDealerNum(smsQuota);
	}
}
